package com.example.executordemo;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    Runnable command = new Runnable() {
        @Override
        public void run() {
            SystemClock.sleep(2000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      /*  Intent intent = new Intent(this, TCPService.class);
        intent.putExtra("task_action", "Task1");
        startService(intent);
        intent.putExtra("task_action", "Task2");
        startService(intent);
        intent.putExtra("task_action", "Task3");
        startService(intent);*/

        /*new Thread(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "run: name:" + Thread.currentThread().getName());
                new DownloadFileTask("AsyncTask#1").execute("");
            }
        }).start();*/
        /*new DownloadFileTask("AsyncTask#1").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"");
        new DownloadFileTask("AsyncTask#2").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"");
        new DownloadFileTask("AsyncTask#3").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"");
        new DownloadFileTask("AsyncTask#4").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"");
        new DownloadFileTask("AsyncTask#5").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"");*/


        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
        fixedThreadPool.execute(command);

        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        cachedThreadPool.execute(command);

        ScheduledExecutorService scheduledThreadPol = Executors.newScheduledThreadPool(4);
        //2000ms后执行command
        scheduledThreadPol.schedule(command, 2000, TimeUnit.MILLISECONDS);
        //延迟10ms后，每隔1000ms执行一次command
        scheduledThreadPol.scheduleAtFixedRate(command, 10, 1000, TimeUnit.MILLISECONDS);

        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        singleThreadExecutor.execute(command);
    }


    private class DownloadFileTask extends AsyncTask<String, Integer, String> {
        private String mName;

        public DownloadFileTask(String name) {
            this.mName = name;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.e(TAG, "onPreExecute: name:" + Thread.currentThread().getName());
        }

        @Override
        protected String doInBackground(String... string) {
            Log.e(TAG, "doInBackground: name:" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return mName;
        }

        @Override
        protected void onPostExecute(String result) {
            Log.e(TAG, "onPostExecute: name:" + Thread.currentThread().getName());
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Log.e(TAG, result + "execute finish at:" +df.format(new Date()));
        }

    }

}
